<?php
/**
 * Created by PhpStorm.
 * User: ZGL
 * Date: 2020/8/24
 * Time: 9:30
 */

namespace App\Http\Controllers\Console;
use App\Http\Controllers\Controller;
use App\Model\ShopModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\RedisService;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;


class PurviewController extends Controller
{
    /**添加编辑权限
        id：权限id,编辑时传递此参。
        name:权限名称
     * parent_id:上级菜单id
     * @param Request $request
     * @return array
     */
    public function act_purview(Request $request){

        $name = $request->input('name')? $request->input('name'):'';
        $parent_id = $request->input('parent_id')? $request->input('parent_id'):0;
        if($name == ""){
            return ["code"=>0,"msg"=>"保存失败，请检查数据格式是否正确"];
        }

        $data = [ 'name' => $name, 'parent_id'=>$parent_id];
        if($request->input("id")) {
            $mid = DB::table("admin_purview")->where("id", $request->input("id"))->update($data);
        }else {
            $mid = DB::table("admin_purview")->insertGetId($data);
        }

        if($mid>0)
        {
            $arr=["code"=>200,"msg"=>"保存成功"];
        }else
            $arr=["code"=>1001,"msg"=>"保存失败，请检查数据格式是否正确"];

        return $arr;

    }

    /**获取角色权限列表
     * @param Request $request
     * @return array
     */
    public function purview_lists(Request $request){

        $purview = DB::select('select p.id,p.name,group_concat(m.name) as menu from admin_purview as p left join admin_purview_menu as rp on p.id = rp.admin_purview_id left join admin_menu as m on rp.admin_menu_id = m.id and m.is_show = 1 group by p.name,p.id order by id asc');

        if($purview){
            $arr=["code"=>200,"msg"=>"获取数据成功","data"=>$purview];
        }else
            $arr=["code"=>0,"msg"=>"获取数据失败"];

        return $arr;
    }


    /**删除角色权限
     * @param Request $request
     * @return array
     */
    public function del_purview(Request $request){

        $id=$request->input("id");
        if($id==""){
            $arr=["code"=>1003,"msg"=>"id不能为空"];
            return $arr;
        }
        //判断角色权限是否还在使用中
        $purview_id=DB::table('admin_role_purview')->where('admin_purview_id','=',$id)->first();
        if($purview_id){
            $arr=["code"=>1004,"msg"=>"角色权限不允许删除"];
            return $arr;
        }
        $re=DB::table('admin_purview')->where("id",$id)->delete();
        $re2=DB::table('admin_purview')->where("parent_id",$id)->delete();
        if($re>0 || $re2>0){
            $arr=["code"=>200,"msg"=>"删除成功"];
        } else
            $arr=["code"=>1001,"msg"=>"删除失败,请检查数据格式"];

        return $arr;
    }
    /**权限菜单控制
     * @param Request $request
     * @return array
     */
    public function act_purview_menu(Request $request){

        $id = $request->input('id')? $request->input('id'):'';
        //菜单接收
        $menu = $request->input('menu')?$request->input('menu'):'';

        if($id == "" || $menu == ""){
            return ["code"=>0,"msg"=>"请检查数据格式是否正确"];
        }
        //查找权限是否存在
        $role_res = DB::table('admin_purview')->select('id')->where('id','=',$id)->first();
        if(!$role_res){
            return ["code"=>1002,"msg"=>"角色不存在，请检查数据格式"];
        }
        //删除原有权限菜单
        $re = DB::table('admin_purview_menu')->where('admin_purview_id',$id)->delete();
        //循环添加新的权限菜单
        foreach ($menu as $k => $v){

            $mid = DB::table("admin_purview_menu")->insertGetId(['admin_purview_id'=>$id,'admin_menu_id'=>$v]);
            if(!$mid) break;
        }

        if($mid)
        {
            $arr=["code"=>200,"msg"=>"保存成功"];
        }else
            $arr=["code"=>0,"msg"=>"保存失败，请检查数据格式是否正确"];

        return $arr;

    }

    /**菜单下拉列表（可用）
     * @param Request $request
     * @return array
     */
    public function memus(Request $request){
        $menu = DB::table('admin_menu')->select('id','name')->where('is_show','=',1)->where('parent_id','=',0)->orderBy('sort','desc')->get()->toArray();
        //得到顶级菜单
        $data =array();
        if($menu){
            foreach ($menu as $k => $row){
                //获取下级菜单和权限
                $menu_sub = DB::table('admin_menu')->select('id','name')->where('is_show','=',1)->where('parent_id','=',$row->id)->orderBy('sort','desc')->get()->toArray();

                if($menu_sub)
                    $row->children=$menu_sub;
                $data[]=$row;
            }
        }

        if($data){
            $arr=["code"=>200,"msg"=>"获取数据成功","data"=>$data];
        }else
            $arr=["code"=>1001,"msg"=>"获取数据失败"];

        return $arr;
    }

}